package com.qtummatrix.controller;

import com.opensymphony.xwork2.ActionSupport;
import com.qtummatrix.entity.QuestionsEntity;
import com.qtummatrix.service.QuestionBankService;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("questionsBankAction")
@Scope("prototype")
public class QuestionsBankController extends ActionSupport {
    private Map<String, Object> info = new HashMap<String, Object>();
    private QuestionsEntity questionsEntity;

    public Map<String, Object> getInfo() {
        return info;
    }

    public void setInfo(Map<String, Object> info) {
        this.info = info;
    }

    public QuestionsEntity getQuestionsEntity() {
        return questionsEntity;
    }

    public void setQuestionsEntity(QuestionsEntity questionsEntity) {
        this.questionsEntity = questionsEntity;
    }

    @Autowired
    @Qualifier("questionBankeService")
    private QuestionBankService questionBankService;

    public String addQuestions() {
        String msg = questionBankService.addQuestion(questionsEntity);
        info.put("msg", msg);
        return SUCCESS;
    }

    public String getQuestionlist() {
        HttpServletRequest request = ServletActionContext.getRequest();
        int scopeid = Integer.parseInt(request.getParameter("scopeid"));
        int typeid = Integer.parseInt(request.getParameter("typeid"));
        String content = request.getParameter("content");
        int n = Integer.parseInt(request.getParameter("n"));
        List<QuestionsEntity> list = questionBankService.getQuestions(scopeid, typeid, content, n);
        info.put("questionslist", list);
        return SUCCESS;
    }

    public String getSumpage() {
        HttpServletRequest request=ServletActionContext.getRequest();

        int scopeid = Integer.parseInt(request.getParameter("scopeid"));
        int typeid = Integer.parseInt(request.getParameter("typeid"));
        String content = request.getParameter("content");

        int sumpage = questionBankService.get_sumpage(scopeid,typeid,content);
        info.put("sumpage", sumpage);
        return SUCCESS;
    }

    public String delQuestions(){
        HttpServletRequest request=ServletActionContext.getRequest();
        int id=Integer.parseInt(request.getParameter("questionsid"));
        questionBankService.delQuestions(id);
        info.put("msg","删除成功！");
        return SUCCESS;
    }
}
